﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickVal.Models;
using QuickVal.Helpers;

namespace QuickVal.Controllers
{
    public class WidgetController : Controller
    {
        [HttpGet]
        public ActionResult CreateMany()
        {
            var widgets = new List<Widget>();
            widgets.Add(new Widget() { Id = 1, IsRequired = true });
            widgets.Add(new Widget() { Id = 2, IsRequired = true });
            return View(widgets);
        }

        [HttpPost]
        public ActionResult CreateMany(IEnumerable<Widget> widgets)
        {
            if (ModelState.IsValid)
            {
                foreach (var widget in widgets)
                    TempData["DataCollected"] = (TempData["DataCollected"] == null) ? widget.Name : TempData["DataCollected"] + ", " + widget.Name;
                return RedirectToAction("Thanks");
            }
            return View(widgets);
        }

        [HttpGet]
        public AjaxViewResult AddWidget()
        {
            Random random = new Random();
            return new AjaxViewResult("Widget", new Widget() { Id = random.Next(0, 1000), IsRequired = true, ErrorMessage = "* Required (delivered from Ajax)" });
        }

        [HttpGet]
        public ViewResult Thanks()
        {
            return View();
        }
    }
}
